package com.victualler.core.config.permission.service.impl;

import com.victualler.core.base.service.impl.BaseServiceImpl;
import com.victualler.core.config.permission.dto.request.UserRoleSaveRequestDto;
import com.victualler.core.config.permission.entity.UserRoleDao;
import com.victualler.core.config.permission.repository.UserRoleQueryRepository;
import com.victualler.core.config.permission.repository.UserRoleRepository;
import com.victualler.core.config.permission.service.UserRoleService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Guoshichang
 * @Date: Create in 17:03 2018/4/5
 * @Discription:  Dict字典类型对应的服务层
 */
@Service
public class UserRoleServiceImpl extends BaseServiceImpl<UserRoleDao,UserRoleRepository,UserRoleQueryRepository> implements UserRoleService {

    /**
     * @author Guoshichang
     * @since 21:46 on 2018/9/9.
     * @param userRoleSaveRequestDto
     * @Function  保存角色的权限列表
     */
    public void saveRoleByUserId(UserRoleSaveRequestDto userRoleSaveRequestDto) {
        Long userId = userRoleSaveRequestDto.getUserId();
        //删除角色原有权限列表
        List<UserRoleDao> oldList = this.baseRepository.findByUserId(userId);
        this.baseRepository.delete(oldList);
        //重新构建权限列表
        List<UserRoleDao> userRoleDaoList = new ArrayList<>();
        for (Long roleId : userRoleSaveRequestDto.getRoleIds()) {
            userRoleDaoList.add(new UserRoleDao(userId,roleId));
        }
        this.baseRepository.save(userRoleDaoList);
    }
}
